package com.jy.wms.controller;


import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.RedisUtils;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.UserType;
import com.jy.wms.pojo.SysGys;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.service.LoginService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;


/**
 * @ClassName: UserInfoController
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @Author zzp
 * @Date 2017/7/27 14:12
 */
@RestController
@RequestMapping("/userInfo")
@Api(value = "/userInfo", description = "关于查询session信息操作")
public class SysUserInfoController{
    @Autowired
    LoginService loginService;
    @Autowired
    RedisUtils redisUtils;
    @ApiOperation(notes = "查询session的用户信息和菜单信息", httpMethod = "GET", value = "查询session的接口")
    @RequestMapping(value = "",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public  @ResponseBody
    JSONObject userInfo(HttpSession session)  {
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        redisUtils.set("sessionId",session.getId());
        return getJsonObject(session, userinfo);
    }

    private JSONObject getJsonObject(HttpSession session, UserInfo userinfo) {
        JSONObject jsonObject = getJsonHz(session, userinfo);
        getJsonGYS(session, userinfo, jsonObject);
        getJsonMenu(userinfo, jsonObject);
        return getJsonReturn(userinfo, jsonObject);
    }

    private JSONObject getJsonReturn(UserInfo userinfo, JSONObject jsonObject) {
        String users=com.alibaba.fastjson.JSONObject.toJSONString(userinfo.getUser());
        jsonObject.accumulate("user",users);
        jsonObject.accumulate("app",userinfo.getApp());
        jsonObject.accumulate("auth",userinfo.getAuth());
        return jsonObject;
    }

    private void getJsonGYS(HttpSession session, UserInfo userinfo, JSONObject jsonObject) {
        if(userinfo.getDatasetvalues().get(UserType.supplier.getType())!=null){
            List<SysGys> gys=loginService.getGys(userinfo.getDatasetvalues().get(UserType.supplier.getType()).get(0));
            jsonObject.accumulate("gys",gys);
            session.setAttribute(BaseSessionInfo.SessionGys,gys);
        }else{
            jsonObject.accumulate("gys","");
        }

    }

    private void getJsonMenu(UserInfo userinfo, JSONObject jsonObject) {
        if(userinfo.getAuth().size()>0){
            com.alibaba.fastjson.JSONArray sysMenu=loginService.selectMenu(userinfo.getAuth());
            jsonObject.accumulate("menu",sysMenu.toString());
        }else{
            jsonObject.accumulate("menu","");
        }
    }

    private JSONObject getJsonHz(HttpSession session, UserInfo userinfo) {
        JSONObject jsonObject=new JSONObject();
        List<SysHz> hz=null;
        if(session.getAttribute(BaseSessionInfo.SessionConsignor)==null){
            if(userinfo.getDatasetvalues().get(UserType.owner.getType())!=null&&userinfo.getDatasetvalues().get(UserType.owner.getType()).size()>0){
                if(userinfo.getDatasetvalues().get(UserType.owner.getType()).get(0).equals("ck_all")){
                    hz=loginService.getHz(null,userinfo.getUser().getOrgId().get(0).toString());
                }else{
                    hz=loginService.getHz(userinfo.getDatasetvalues().get(UserType.owner.getType()),"");
                }
                session.setAttribute(BaseSessionInfo.SessionConsignor,hz);
            }
        }else{
            hz= (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
        }
        jsonObject.accumulate("hz",hz==null?"":hz);
        return jsonObject;
    }

    /**
     * 用户退出操作
     * @return
     */
    @ApiOperation(notes = "用户退出操作", httpMethod = "GET", value = "用户退出")
    @RequestMapping(value = "/logout",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public  @ResponseBody  JSONObject logout(HttpSession session){
        try{
            session.invalidate();
            return SetStatus.setFunctionType(1,"success","退出成功","退出成功","");
        }catch (Exception e){
            return SetStatus.setFunctionType(1,"error","退出失败","系统异常","");
        }

    }

    @RequestMapping(value = "/getUserSession",method = RequestMethod.GET,produces = {"application/json;charset=UTF-8"})
    public  @ResponseBody
    UserInfo userSessionInfo(HttpSession session)  {
        UserInfo userinfo= (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
        return userinfo;
    }

}
